/** index.js By Beginner Emain:zheng_jinfan@126.com HomePage:http://www.zhengjinfan.cn */
layui.config({
    base: 'content/layui-market/js/'
}).use(['element', 'layer', 'navbar', 'tab'], function () {
    var element = layui.element(),
		$ = layui.jquery,
		layer = layui.layer,
		navbar = layui.navbar(),
		tab = layui.tab({
		    elem: '.admin-nav-card' //设置选项卡容器
		});
    $.ajax({
        type: "GET",
        url: "../Home/CheckLockUser",
        dataType: "json",
        success: function (data) {
            if (data.Data.lockStatus) {
                lock($, layer);
            }
        }
    });
    //iframe自适应
    $(window).on('resize', function () {
        var $content = $('.admin-nav-card .layui-tab-content');
        $content.height($(this).height() - 147);
        $content.find('iframe').each(function () {
            $(this).height($content.height());
        });
    }).resize();
    $('#menu').children().each(function () {
        var $this = $(this);
        //绑定一级导航的点击事件
        $this.on('click', function () {
            //获取设置的模块ID
            var id = $this.data('module-id');
            //这里的数据源只是演示时用的，实际需求可能通过远程读取（根据模块ID来获取对应模块的信息）

            if (id===0) {
                return false;
            }
            //设置navbar
            navbar.set({
                spreadOne: true,
                elem: '#admin-navbar-side', //存在navbar数据的容器ID
                cached: false,
                url: "../Home/GetMenu?menuId=" + id
            });
            //渲染navbar
            navbar.render();
            //监听点击事件
            navbar.on('click(side)', function (data) {
                tab.tabAdd(data.field);
            });
        });

    });
    //模拟点击第一个
    $('#menu').children()[0].click();

    //左侧菜单展开
    $('.admin-side-toggle').on('click', function () {
        var sideWidth = $('#admin-side').width();
        if (sideWidth === 200) {
            $('#admin-body').animate({
                left: '0'
            }); //admin-footer
            $('#admin-footer').animate({
                left: '0'
            });
            $('#admin-side').animate({
                width: '0'
            });
        } else {
            $('#admin-body').animate({
                left: '200px'
            });
            $('#admin-footer').animate({
                left: '200px'
            });
            $('#admin-side').animate({
                width: '200px'
            });
        }
    });8

    //锁屏
    $(document).on('keydown', function () {
        var e = window.event;
        if (e.keyCode === 76 && e.altKey) {
            $.ajax({
                type: "GET",
                url: "../Home/LockUser",
                dataType: "json",
                success: function (data) {
                    if (!data.IsError) {
                        lock($, layer);
                    }
                }
            });


        }
    });
    $('#lock').on('click', function () {

        $.ajax({
            type: "GET",
            url: "../Home/LockUser",
            dataType: "json",
            success: function (data) {
                if (!data.IsError) {
                    lock($, layer);
                }
            }
        });
    });

    //手机设备的简单适配
    var treeMobile = $('.site-tree-mobile'),
		shadeMobile = $('.site-mobile-shade');
    treeMobile.on('click', function () {
        $('body').addClass('site-mobile');
    });
    shadeMobile.on('click', function () {
        $('body').removeClass('site-mobile');
    });
});

function lock($, layer) {
    layer.open({
        title: false,
        type: 1,
        closeBtn: 0,
        anim: 6,
        content: $('#lock-temp').html(),
        shade: [0.9, '#393D49'],
        success: function (layero, lockIndex) {
            //锁屏

            //给显示用户名赋值
            layero.find('div#lockUserName').text('admin');
            layero.find('input[name=lockPwd]').on('focus', function () {
                var $this = $(this);
                if ($this.val() === '输入密码解锁..') {
                    $this.val('').attr('type', 'password');
                }
            })
				.on('blur', function () {
				    var $this = $(this);
				    if ($this.val() === '' || $this.length === 0) {
				        $this.attr('type', 'text').val('输入密码解锁..');
				    }
				});
            //在此处可以写一个请求到服务端删除相关身份认证，因为考虑到如果浏览器被强制刷新的时候，身份验证还存在的情况			
            //do something...
            //e.g. 
            /*
			 $.post(url,params,callback,'json');
			 */
            //绑定解锁按钮的点击事件
            $(document).on('keydown', function () {
                var e = window.event;
                if (e.keyCode === 13) {
                    layero.find('button#unlock').click();
                }
            });
            layero.find('button#unlock').on('click', function () {
                unlock();
            });
            /**
			 * 解锁操作方法
			 * @param {String} 用户名
			 * @param {String} 密码
			 */
            var unlock = function () {
                var $lockBox = $('div#lock-box');
                var pwd = $lockBox.find('input[name=lockPwd]').val();
                if (pwd === '输入密码解锁..' || pwd.length === 0) {
                    layer.msg('请输入密码..', {
                        icon: 2,
                        time: 1000
                    });

                    return;
                }
                $.ajax({
                    type: "Post",
                    url: "../Home/UnLockUser",
                    dataType: "json",
                    data: { password: pwd },
                    success: function (data) {
                        if (!data.IsError) {
                            layer.close(lockIndex);
                        }
                        else {
                            layer.msg(data.Message, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                });
            };
        }
    });
};